$(document).ready(function(){
    $(".new_products .pagination a").click(pageClickHandle);    
    var hash = window.location.hash;
    if(hash){
        var url = window.location.href;
        var array1 = url.split("/");
        var lastEle = array1[array1.length -1];
        var array2 = lastEle.split("#");
        action = array2[0];
        if(action =='book'||action=='discount'){
            var p = array2[1].substr(0,1);
            var page = array2[1].substring(1);
            if(p != "p" || isNaN(page)){
                var str = "Please enter a valid Url";
                $(".new_products").prepend(str);
                
            }else{                         
                 showBook(action, page);
                 
            }    
        }           
    }else{
        var url = window.location.href;
        var array = url.split("/");
        var action = array[array.length-1];
        if(action =='book' || action =='discount'){
            showBook(action, 1);
                
        }        
    }
        
});
function showBook(action, page){
    
    if(action =='book'){
        $(".new_prod_box").remove();    
    }else if(action =='discount'){
        $(".feat_prod_box").remove();
    }else {
        alert("ErrorController");
    }
    $.ajax({        
        url: ajaxUrl+"model/pagingbook.php?action="+action+"&page="+page+"&hash=hash",
        success: function(data){
            $(".new_products").prepend(data);
        }
    });
}
function showPaging(action, page){
  /*  $(".left_content").load(ajaxUrl+"model/pagingBook.php?action="+action+"&page="+page);*/
    if(action =='book'){
        $(".new_prod_box").remove();    
    }else if(action =='discount'){
        $(".feat_prod_box").remove();
    }else {
        alert("ErrorController");
    }
    
    $.ajax({        
        url: ajaxUrl+"model/pagingbook.php?action="+action+"&page="+page,
        success: function(data){
            $(".new_products").prepend(data);
        }
    });			
};
function pageClickHandle(event){
	var url = $(this).attr("href");
	var array = url.split("/");
    var length = array.length;    
	var action  = array[length-2];;
	var page = array[length-1]; ;
    window.location.hash = "#p"+page;  
	event.preventDefault();
    updatePageHandle(action, page);	
	showPaging(action, page);
}
function updatePageHandle(action, pg){
    var page = parseInt(pg, 10);
    var lastUrl = $(".new_products .pagination a:last").attr("href");
	var lastArray = lastUrl.split("/");
    var length = lastArray.length;
    var numOfPage = lastArray[length-1];
//	numOfPage = parseInt(numOfPage, 10);
    
    var str = '';
    if(page == 1){
        var i = 0;
        for(i = page ; i < page + 3; i++){
            if(i <= numOfPage){
                if(i == page){
                    str += '<a href="'+ajaxUrl+action+'/' +i+ '"><span class="current">' +i+ '</span></a>';
                }else{
                    str += '<a href="'+ajaxUrl+action+'/' +i+ '">' +i+ '</a>';    
                }                 
            }            
        }
        if(i < numOfPage){
            str += "...";
        }
        var next = page + 1;
        str += '<a href="'+ajaxUrl+action+'/' +next+ '"> &gt;&gt; </a>';
        str += '<a href="'+ajaxUrl+action+'/' +numOfPage+ '"> [Last] </a>';
        
        	
    }else if(page == numOfPage){
        
        var previous = page - 1;
        str ='<a href="'+ajaxUrl+action+'/1">[First]</a>';
        str +='<a href="'+ajaxUrl+action+'/' +previous+'">&lt;&lt;</a>';
        var i = page - 2;
        if(i > 1){
            str += "...";
        }
        for(i= page - 2; i <= numOfPage; i++){
            if(i >= 1 && i <= numOfPage){
                if(i == page){
                    str += '<a href="'+ajaxUrl+action+'/' +i+ '"><span class="current">' +i+ '</span></a>';
                }else{
                    str += '<a href="'+ajaxUrl+action+'/' +i+ '">' +i+ '</a>';    
                }                    
            }            
        }    
       
        
    
    }else{
        
        var previous = page - 1;
        var next = page + 1;
        str +='<a href="'+ajaxUrl+action+'/1">[First]</a>';
        str +='<a href="'+ajaxUrl+action+'/' +previous+ '">&lt;&lt;</a>';
        var i = page-1;
        for(var j = 0 ; j < 3; j++){
            if(i <= numOfPage){
                if(i == page){
                    str += '<a href="'+ajaxUrl+action+'/' +i+ '"><span class="current">' +i+ '</span></a>';
                }else{
                    str += '<a href="'+ajaxUrl+action+'/' +i+ '">' +i+ '</a>';    
                }    
            }
            i++;                
        }
        if(i < numOfPage){
            str += "...";
        }
        
        str += '<a href="'+ajaxUrl+action+'/' +next+ '"> &gt;&gt; </a>';
        str += '<a href="'+ajaxUrl+action+'/' +numOfPage+ '"> [Last] </a>';
         
        
    }
    
     str += '<script type="text/javascript">'+
                '$(document).ready(function(){'+
                     '$(".new_products .pagination a").click(pageClickHandle);'+   
                '});'+
            '</script>';
    
    $(".pagination a").remove();
    $(".pagination").html(str);    
       
}
